$(document)
		.ready(
				function() {
					var contextPath = document.location.pathname;
					var index = contextPath.substr(1).indexOf("/");
					contextPath = contextPath.substr(1, index);
					var basePath = "/" + contextPath;
					var totalCount = $("#totalCount").val();
					var productInfor;
					var pSize = $("#pageSize").val();
					var currentPage=1;
					var uploader;
					var pageIndex = $("#pageIndex").val();
					var pageCount = $("#pageCount").val();
					if (pageCount == null || pageCount == ""
							|| pageCount == "undefined") {
						pageCount = 1;
						currentPage=1;

					}
					if (totalCount == null || totalCount == ""
						|| totalCount == "undefined") {
						totalCount = 0;

				}
					
					
					$('form')
							.submit(
									function() {
										if (typeof jQuery.data(this,
												"disabledOnSubmit") == 'undefined') {
											jQuery.data(this,
													"disabledOnSubmit", {
														submited : true
													});
											$(
													'input[type=submit], input[type=button]',
													this).each(
													function() {
														$(this).attr(
																"disabled",
																"disabled");
													});
											return true;
										} else {
											return false;
										}
									});

					$("#optBtn").delegate(
							'#saveBtn',
							'click',
							function() {

								$("#modualDetail").attr("method", "post");

								var proId = $(
										"#modualDetail input[id='proId']")
										.val();
								// alert(proId);
								if (proId == "" || proId == null
										|| proId == "undefined") {

									$("#modualDetail").attr("action",
											"save.do");
									
								} else {
									$("#modualDetail").attr("action",
											"update.do");
									
								}
								$("#modualDetail").submit();

							});
				
					$('#pagination2')
							.jqPaginator(
									{
//										totalPages : pageCount,
//										visiblePages : pageCount,
										totalCounts:totalCount,
										pageSize : pSize,
										visiblePages : pageCount,
										currentPage:1,

										prev : '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
										page : '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
										next : '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
										onPageChange : function(num, type) {
											
											$("#pageIndex").val(num);
											var typeId = $("#proTypeId").val();
											// alert("type----Id="+typeId);
											var queryUrl = "queryByPage.do";
											$
													.ajax({
														url : queryUrl,
														dataType : 'json',
														data : {
															pageIndex : num,
															pageSize : pSize
															
														},
														cache : false,
														success : function(data) {

															$(
																	"#products tr:not(:first)")
																	.remove();
															var results = data.pageResult.resultList;
															for (i = 0; i < results.length; i++) {
																var product = results[i];

																var rowContent = "<tr>"
																		+ "<td><input name='proId' class='checkbox' type='checkbox' value='"
																		+ product.id
																		+ "'/></td>"
																		+ "<td>"
																		+ product.id
																		+ "</td>"
																		+ "<td>"
																		+ product.modualContent.substring(0,40)
																		+ "</td>"
																		+ "<td>"
																		+ product.bussinessName
																		+ "</td>"
																rowContent = rowContent
																		+ "<td><button name='editBtn' objId='"
																		+ product.id
																		+ "' class='btn btn-sm-block'>修改</button></td>"
																		+ "</tr>";
																$("#products")
																		.append(
																				rowContent);
															}

														},
														error : function(html) {
															return;
														}
													});
										}
									});
					

					/**
					 * 全选当前页记录
					 */
					$("#products").delegate('#selectAll', 'click', function() {
						var isSelectAll = $(this).prop('checked');
						$("input[name='proId']").each(function() {
							$(this).prop("checked", isSelectAll);
						});
					});

					/**
					 * 修改按钮事件
					 */

					$("#products").delegate('[name="editBtn"]', 'click',
							function() {
								var proId = $(this).attr("objId");

								var editUrl = "show.do";
								$.ajax({

									url : editUrl,
									dataType : 'json',
									data : {
										id : proId

									},
									cache : false,

									success : function(data) {
										$("#pic").attr("readonly",true);
										loadModual(data);
									},
									error : function(html) {
										// alert(html);
										alert("Load Data failly");
										refresh();
									}
								});

							});

					$("#products").delegate('[name="viewResource"]', 'click',
							function() {
								var proId = $(this).attr("objId");
							

								loadResource(proId, 0);
							});

					$("#products").delegate(
							'[name="viewRule"]',
							'click',
							function() {
								var proId = $(this).attr("objId");

								var editUrl = basePath
										+ "/manage/product/proDetails/list.do";
								document.location.href = editUrl + "?id="
										+ proId;
							});

					$("#tools").delegate('#deleteBtn', 'click', function() {
						delChk = $('input[name="proId"]:checked');

						if (delChk.length > 0) {
							if (confirm("Are you sure delete the record?")) {

								var ids = "";
								/* var delUrl = "delete.do?ids="; */
								var delUrl = "delete.do";
								var idArr = new Array();

								for (var i = 0; i < delChk.length; i++) {
									idArr.push(delChk.get(i).value);

								}

								$.ajax({
									url : delUrl,
									data : {
										ids : idArr.toString()
									},
									cache : false,

									success : function(html) {
										alert("Delete successfully");
										refresh();
									},
									error : function(html) {
										alert("Delete failly");
										refresh();
									}
								});
							} else {
								return;
							}
						} else {
							alert("Please select the record to delete");
							return;
						}
					});

					
					$("#businessType").delegate($(this),'change',function(){
						
						var businessCode=$(this).val();
						var businessName=$(this).find("option:selected").text();
						
						if(businessCode!="" && businessName!="请选择")
							{
							 $("#bussinessCode").val(businessCode);       
							 $("#bussinessName").val(businessName);   
							}
					
					});
					
					
					
					$("#tools").delegate('#addProduct', 'click', function() {

						var typeId = $("#proTypeId").val();
						// alert("******proId******"+typeId);
						var editUrl = "add.do";
						$.ajax({

							url : editUrl,
							dataType : 'json',
							data : {
								id : typeId
							},
							cache : false,

							success : function(data) {

								// alert("************"+data.typeId);
								$("#modualDetailDialog input").each(function() {
									$(this).prop("value", null);
									
									$(this).removeAttr("readonly");

								});
								$("#proCode").attr("readonly",true);
								$("#pic").attr("readonly",true);
								$("#modualDetailDialog textarea").each(function() {
									$(this).prop("value", null);

								});

								loadModual(data)

							},
							error : function(html) {
								alert(html);
								alert("Load Data failly");
								refresh();
							}
						});

					});
					
					
					
					

					
					
					

					function loadModual(modual) {

						
						$("#proId").val(modual.id);
						
						$("#modualName").val(modual.modualName);
						$("#bussinessName").val(modual.bussinessName);
						$("#bussinessCode").val(modual.bussinessCode);
						$("#businessType").val(modual.bussinessCode);
						$("#modualContent").val(modual.modualContent);
						$("#note").val(modual.note);
						
						$("#modualDetailDialog").modal("show");
					}
					
					
					
					

					function vaildation() {
						 productInfor=	$(".jqtransform").Validform({
							tiptype:function(msg,o,cssctl){
								
								if(!o.obj.is("form")){
									var tds=o.obj.text();
									
									var objtip=o.obj.siblings(".Validform_checktip");
									cssctl(objtip,o.type);
									objtip.text(msg);
									
								}	
								else{
									if(infoObj.is(":visible")){return;}
									var left=o.obj.offset().left,
										top=o.obj.offset().top;
					
									infoObj.css({
										left:left+170,
										top:top-45
									}).show().animate({
										top:top-35	
									},200);
							}
							}
						});		
					productInfor.tipmsg.w["zh1-6"]="请输入1到6个中文字符！";
						
					productInfor.addRule([{
							ele:".inputxt",
							datatype:"*"
						},
						
						
						{
							ele:"select",
							datatype:"*"
						},
						{
							ele:":radio:first",
							datatype:"*"
						},
						{
							ele:":checkbox:first",
							datatype:"*"
						}]);
					}


					
					
					function refresh() {
						window.location.reload();
					}
				});
